<?php

namespace app\admin\model;

use think\Model;


class Articles extends Model
{



    //数据库
    protected $connection = 'database';
    // 表名
    protected $name = 'blog_articles';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }









    public function blogsorts()
    {
        return $this->belongsTo('BlogSorts', 'sorts_ids', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function bloglabels()
    {
        return $this->belongsTo('BlogLabels', 'labels_ids', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function blogcomments()
    {
        return $this->belongsTo('Comments', 'article_comment_count', 'article_id', [], 'LEFT')->setEagerlyType(0);
    }
    public function hotarticlelist()
    {
        //热门列表
        $hotlist = $this->where('status', 1)->limit(8)->order('article_views desc,updatetime')->select();
        foreach ($hotlist as $row) {
           $row->article_content=strip_tags($row['article_content']);
        }
        return $hotlist;
    }
}
